package com.lmj.service.impl;

import com.lmj.domain.Account;
import com.lmj.mapper.AccountMapper;
import com.lmj.service.AccountService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service("accountService")
public class AccountServiceImpl implements AccountService{

    @Autowired
    private AccountMapper accountMapper;
//    注入

    public void save(Account account) {
//        try {
//            InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapperConfig.xml");
//            //SqlSessionFactory是接口不能配得配对应的实现.pom.xnl中mybatist-spring包提供了这个的工厂实现类
//            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);//获得工厂对象,进阶让sqlsessions由spring产生
//            SqlSession sqlSession = sqlSessionFactory.openSession();
//            AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
//            mapper.save(account);
//            sqlSession.commit();//因为是更新操作
//            sqlSession.close();
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        accountMapper.save(account);


    }

    public List<Account> findAll() {
//        try {
//            InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapperConfig.xml");
//            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);//获得工厂对象
//            SqlSession sqlSession = sqlSessionFactory.openSession();
//            AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
//            List<Account> accountList = mapper.findAll();
//            sqlSession.close();
//            return accountList;
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
        List<Account> accountList = accountMapper.findAll();
        return accountList;
    }
}
